package com.selfdriving.utils;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.selfdriving.BaseApplication;
import com.selfdriving.R;
import com.selfdriving.bean.Entity;

/**
 * ViewFlipper的公共类
 * 
 * @author Administrator
 */
public class ViewFlipperView implements OnGestureListener, OnTouchListener
{
	public Context context;
	public ViewFlipper viewFlipper;
	private final int SHOW_NEXT = 0011;
	private static final int FLING_MIN_DISTANCE = 20;
	private static final int FLING_MIN_VELOCITY = 0;
	private boolean showNext = true;
	private int currentPage = 0;
	public ImageView RadioImg[];
	private LinearLayout RadioImages;
	private GestureDetector mGestureDetector;
	private ImageView imageView; // ViewFlipper的图片对象
	private ImageView imageViews[];// ViewFlipper的图片数组
	private boolean isRun = true;
	private int key = -1;// 辨别是否点击viewFlipper下滑
	private String imgPath[] = null;
	private List<? extends Entity> datas = null;
	BaseApplication application;
	TextView textView;
	int listX = 0;
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	protected DisplayImageOptions options;

	/** 图片集合形式获取ViewFlipper **/
	public ViewFlipperView(BaseApplication application, Context context,
			TextView textView, int key, List<? extends Entity> datas, int listX)
	{
		// TODO Auto-generated constructor stub
		this.context = context;
		this.textView = textView;
		this.listX = listX;
		this.textView.setVisibility(View.VISIBLE);
		this.application = application;
		this.key = key;
		this.datas = datas;
		if (datas.size() == 0)
		{
			imgPath = new String[1];
		} else
		{
			imgPath = new String[datas.size()];
		}
		initUI();
		getImages();
		setCount(0);
		isRun();
	}

	/** 图片集合形式获取ViewFlipper **/
	public ViewFlipperView(BaseApplication application, Context context,
			LinearLayout RadioImages, int key, List<? extends Entity> datas)
	{
		// TODO Auto-generated constructor stub
		this.listX = listX;
		this.context = context;
		this.RadioImages = RadioImages;
		this.application = application;
		this.key = key;
		this.datas = datas;
		if (RadioImages.getChildCount() > 0)
		{// 清除过期点点
			RadioImages.removeAllViews();
		}
		if (datas.size() == 0)
		{
			imgPath = new String[1];
		} else
		{
			imgPath = new String[datas.size()];
		}
		initUI();
		getImages();
		// getRadioImages();
		isRun();
	}

	/** 图片数组形式获取ViewFlipper **/
	public ViewFlipperView(BaseApplication application, Context context,
			TextView textView, LinearLayout RadioImages, int key,
			String imgPath[])
	{
		// TODO Auto-generated constructor stub
		this.context = context;
		this.RadioImages = RadioImages;
		this.textView = textView;
		this.key = key;
		this.application = application;
		if (RadioImages.getChildCount() > 0)
		{
			RadioImages.removeAllViews();
		}
		this.imgPath = imgPath;
		initUI();
		getImages();
		// getRadioImages();
		isRun();
	}

	/**
	 * 设置静态本地图片到ViewFilpper
	 * 
	 * @param id
	 */
	public void setViewFilpperImg(int[] id)
	{
		viewFlipper = new ViewFlipper(context);
		viewFlipper.setOnTouchListener(this);
		viewFlipper.setLongClickable(true);
		mGestureDetector = new GestureDetector(context, this);
		imgPath = new String[id.length];
		for (int i = 0; i < id.length; i++)
		{
			ImageView imageView = new ImageView(context);
			Bitmap bit = PhotoUtil.getBitmap(context, id[i]);
			imageView.setImageBitmap(bit);
			imageView.setScaleType(ScaleType.CENTER_CROP);
			viewFlipper.addView(imageView);
		}
	}

	public void initUI()
	{
		viewFlipper = new ViewFlipper(context);
		viewFlipper.setOnTouchListener(this);
		viewFlipper.setLongClickable(true);
		imageViews = new ImageView[imgPath.length];
		mGestureDetector = new GestureDetector(context, this);
		switch (key)
		{
		case Constant.VF_HOTEL_DETAILS:
			if (datas.size() != 0)
			{
				imgPath = application.viewImg;
				imageViews = new ImageView[imgPath.length];
			}
			break;
		}
	}

	public void isRun()
	{
		if (RadioImages != null && imgPath.length > 1
				&& viewFlipper.getChildCount() > 1)
		{
			RadioImages.setVisibility(View.VISIBLE);
			thread.start();
		} else if (RadioImages != null)
		{
			RadioImages.setVisibility(View.GONE);
		} else if (textView != null)
		{
			if (application.viewImg.length > 1)
			{
				thread.start();
			}
		}
	}

	public void getImgLoad()
	{
		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.default_loading)
				.showImageForEmptyUri(R.drawable.default_loading)
				.showImageOnFail(R.drawable.default_loading)
				.cacheInMemory(true).cacheOnDisc(true).considerExifParams(true)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
	}

	public void getImages()
	{
		getImgLoad();
		for (int i = 0; i < imageViews.length; i++)
		{
			imageView = new ImageView(context);
			imageViews[i] = imageView;
			imageViews[i].setScaleType(ScaleType.CENTER_CROP);
			if (imgPath[0] == null && datas.size() == 0 && key != 5)
			{
				ImageView view = new ImageView(context);
				view.setImageResource(R.drawable.default_loading);
				viewFlipper.addView(view);
			} else if (imgPath[i] == null && datas.size() == 0)
			{

			} else
			{
				imageLoader.displayImage(imgPath[i], imageView, options);
				viewFlipper.addView(imageViews[i]);
			}
		}
	}

	public void setCount(int x)
	{
		textView.getBackground().setAlpha(99);
		textView.setVisibility(View.VISIBLE);
		textView.setText((x + 1) + "/" + imgPath.length);
	}

	@SuppressLint("HandlerLeak")
	Handler mHandler = new Handler()
	{
		@Override
		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
			case SHOW_NEXT:
				if (showNext)
				{
					showNextView();
				} else
				{
					showPreviousView();
				}
				break;
			}
		}
	};
	Thread thread = new Thread()
	{

		@Override
		public void run()
		{
			while (isRun)
			{
				try
				{
					Thread.sleep(1000 * 3);
					Message msg = new Message();
					msg.what = SHOW_NEXT;
					mHandler.sendMessage(msg);
				} catch (InterruptedException e)
				{
					e.printStackTrace();
				}
			}
		}
	};

	private void showNextView()
	{
		viewFlipper.setInAnimation(AnimationUtils.loadAnimation(context,
				R.anim.push_left_in));
		viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(context,
				R.anim.push_left_out));
		viewFlipper.showNext();
		currentPage++;
		setCount(viewFlipper.getDisplayedChild());
	}

	private void showPreviousView()
	{
		viewFlipper.setInAnimation(AnimationUtils.loadAnimation(context,
				R.anim.push_right_in));
		viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(context,
				R.anim.push_right_out));
		viewFlipper.showPrevious();
		currentPage--;
		setCount(viewFlipper.getDisplayedChild());
	}

	@Override
	public boolean onTouch(View v, MotionEvent event)
	{
		return mGestureDetector.onTouchEvent(event);
	}

	@Override
	public boolean onDown(MotionEvent e)
	{
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e)
	{
		final int point = viewFlipper.getDisplayedChild();
		Intent intent = null;
		return true;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY)
	{
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY)
	{
		// TODO Auto-generated method stub
		if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE
				&& Math.abs(velocityX) > FLING_MIN_VELOCITY)
		{
			showNextView();
			showNext = true;
		} else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE
				&& Math.abs(velocityX) > FLING_MIN_VELOCITY)
		{
			showPreviousView();
			showNext = false;
		}
		return false;
	}
}
